<template>
  <div class="members">
    <top-header></top-header>

    <div>
      <search-one @search="onSearch"></search-one>
    </div>

    <div class="common-list-body" v-fullheight>
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
          <div class="list-item" v-for="(item, key) in listSource" :key="key">
            <!-- <div class="avatar-div">
              <img :src="item.sendUserPortrait ? `${$config.apiHost}/prod-api${item.sendUserPortrait}` : ''" alt="" />
            </div> -->
            <div class="item-info">
              <div class="name-div">
                <div class="name">{{ item.nickName }}</div>
                <div class="code">{{ item.jobNo }}</div>
              </div>
              <div class="status" :class="`status-${item.status}`">
                <span>{{ personnelStatus[item.status] }}</span>
              </div>
              <div class="mobile">{{ item.phonenumber }}</div>
              <a :href="`tel:${item.phonenumber}`" class="mobile-icon">
                <span class="iconfont icondianhua"></span>
              </a>
            </div>
          </div>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script lang="ts">
import { Component } from "vue-property-decorator";
import TopHeader from "@/components/TopHeader.vue";
import { List, Cell, PullRefresh } from "vant";
import SearchOne from "@/components/SearchOne.vue";
import Api from "@/api";
import Base from "@/utils/base";
import Enum from "@/typings/Enum";

@Component({
  name: "Members",
  components: {
    TopHeader,
    SearchOne,
    VanList: List,
    VanCell: Cell,
    VanPullRefresh: PullRefresh
  }
})
export default class Members extends Base {
  private listSource: any[] = [];
  private loading = false;
  private finished = false;
  private refreshing = false;

  private pageNum = 1;
  private pageSize = 20;
  private sortBy = "status"; // 排序字段
  private order = "asc"; // 排序顺序
  private searchValue = ""; // 消息内容

  private nickName = ""; // 姓名
  private jobNo = ""; // 员工编号
  private phonenumber = ""; // 搜索值
  private personnelStatus = Enum.PersonnelStatus;

  onSearch(event: any) {
    this.searchValue = event;
    this.refreshing = true;
    this.onRefresh();
  }

  onLoad() {
    if (this.finished) {
      return;
    }
    setTimeout(async () => {
      if (this.refreshing) {
        this.pageNum = 1;
        this.refreshing = false;
      }
      const res = await Api.GetPersonnelList(this.listParams);
      if (res && res.code === 200) {
        if (this.pageNum === 1) {
          this.listSource = res.rows;
        } else {
          this.listSource.push(...res.rows);
        }
        if (res.total <= this.listSource.length) {
          this.finished = true;
        }
        this.pageNum++;
      } else {
        this.finished = true;
      }
      this.loading = false;
    }, 500);
  }

  onRefresh() {
    // 清空列表数据
    this.finished = false;

    // 重新加载数据
    // 将 loading 设置为 true，表示处于加载状态
    this.loading = true;
    this.onLoad();
  }

  get listParams() {
    return {
      pageNum: this.pageNum,
      pageSize: this.pageSize,
      sortBy: this.sortBy,
      order: this.order,
      userId: this.currentUser.userId,
      phonenumber: this.phonenumber,
      nickName: this.nickName,
      jobNo: this.jobNo,
      searchValue: this.searchValue
    };
  }
}
</script>

<style lang="less" scoped>
.members {
  .list-item {
    overflow: hidden;
    position: relative;
    margin: 10px 10px 0px 10px;
    background: #ffffff;
    border-radius: 8px;
    padding: 15px;
    display: flex;
    &:last-child {
      border-bottom: 0;
    }

    .item-info {
      display: flex;
      // width: calc(100% - 43px);
      width: 100%;
    }

    .avatar-div {
      height: 43px;
      width: 43px;
      border-radius: 4px;
      overflow: hidden;
    }

    img {
      display: none;
      height: 100%;
      width: 100%;
    }

    .name {
      font-size: 16px;
      color: #2c3038;
    }
    .code {
      color: #797f89;
    }

    .status,
    .mobile,
    .mobile-icon {
      display: flex;
      align-items: center;
    }

    .name-div {
      flex: 1;
      // margin-left: 12px;
      // width: 80px;
    }

    .status {
      margin-left: 18px;
      font-size: 12px;
      position: absolute;
      top: 0;
      right: 0;
      span {
        display: inline-block;
        line-height: 20px;
        height: 20px;
        width: 48px;
        background: rgba(54, 207, 201, 0.14);
        color: #36cfc9;
        text-align: center;
        border-radius: 0px 8px 0px 10px;
      }
      &.status-1 span {
        color: #8b8b8b;
        background: rgba(70, 70, 70, 0.14);
      }
      &.status-2 span {
        color: #ffb700;
        background: rgba(255, 183, 43, 0.14);
      }
    }

    .mobile {
      width: 100px;
      margin-left: 10px;
      color: #797f89;
    }

    .mobile-icon {
      margin-left: 16px;
    }

    .icondianhua {
      color: #5e9efb;
      font-size: 25px;
    }
  }
}
</style>
